package com.rykj.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.immersionbar.ImmersionBar;
import com.rykj.AppProxy;
import com.rykj.R;
import com.rykj.util.CommonUtil;
import com.rykj.widget.TopBar;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * Description: author: zhangsan on 16/7/19 下午2:50.
 */
public abstract class BaseFragment extends ImmersionFragment implements TipCommonMvpView , TopBar.onTopBarClickListener {
    public String TAG = getClass().getSimpleName();

    protected View mContentView;
    protected Context mContext;
    protected boolean isVisible, isPrepared, isFirstIn = true;
    protected AppProxy appProxy;
    protected LoadingDialogHelper loadingDialogHelper;

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden) {
//            isVisible = true;
//            onVisible();
//        } else {
//            isVisible = false;
//        }
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        appProxy = AppProxy.INSTANCE;
        loadingDialogHelper = new LoadingDialogHelper();


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(initLayoutRes(), container, false);
        afterOnCreateView();
        return mContentView;
    }

    protected void afterOnCreateView() {

    }

    protected abstract int initLayoutRes();


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    protected abstract void initView();


    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext = null;
    }

    public void onVisible() {
        if (isVisible && isFirstIn && isPrepared) {
            isFirstIn = false;
            lazyLoad();
        }
    }

    protected void lazyLoad() {

    }

    protected void registerBus() {
        EventBus.getDefault().register(this);
    }

    protected void unRegisterBus() {
        EventBus.getDefault().unregister(this);
    }

    protected <T extends View> T getView(int viewId, View root) {
        return (T) root.findViewById(viewId);
    }

    protected <T extends View> T getView(int viewId) {
        return (T) mContentView.findViewById(viewId);
    }

    protected <T extends View> T getViewFromLayout(@LayoutRes int layout) {
        return (T) LayoutInflater.from(mContext).inflate(layout, null, false);
    }

    protected <T extends View> T getViewFromLayout(@LayoutRes int layout, ViewGroup root, boolean attach) {
        return (T) LayoutInflater.from(mContext).inflate(layout, root, attach);
    }

    public View getContentView() {
        return mContentView;
    }

    public void showActivity(Intent it) {
        startActivity(it);
    }

    /**
     * show to @param(cls)
     */
    public void showActivity(Class<?> cls, Bundle extras) {
        Intent intent = new Intent(mContext, cls);
        intent.putExtras(extras);
        startActivity(intent);
    }

    /**
     * show to @param(cls)
     */
    public void showActivity(Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
    }

    protected void showLoading() {
        showLoading("加载中...");
    }

    @Override
    public void showToast(String msg) {
        CommonUtil.showToast(msg);
    }

    @Override
    public void showLoading(String msg) {
        loadingDialogHelper.showLoading(getActivity(), msg);
    }

    @Override
    public void showLoading(int msg) {
        showLoading(CommonUtil.getString(msg));
    }

    @Override
    public void disMissLoading() {
        loadingDialogHelper.disMiss();
    }




    @Override
    public void onTopBarViewClick(View v) {
        int id = v.getId();
        if (id == R.id.topBarImgBtnLeft) {
            onTopBarLeftImgClick();
        } else if (id == R.id.topBarTitle) {
            onTopBarTitleClick();
        } else if (id == R.id.topBarImgBtnRight) {
            onTopBarRightImgClick();
        } else if (id == R.id.topBarTvRight) {
            onTopBarRightTextClick();
        }
    }

    /**
     * topbar 左边imagebtn 点击响应
     */
    public void onTopBarLeftImgClick() {

    }

    /**
     * topbar 右边imagebtn 点击响应
     */
    public void onTopBarRightImgClick() {

    }

    /**
     * desc: 右边TextView点击响应
     **/
    public void onTopBarRightTextClick() {

    }

    /**
     * topbar 中间 文本 点击响应
     */
    public void onTopBarTitleClick() {

    }

    @Override
    public void initImmersionBar() {
        getImmersionBar().init();
    }

    protected ImmersionBar getImmersionBar() {
        return ImmersionBar.with(this).statusBarDarkFont(true);
    }
}
